﻿Public Class FormUpdate
    Private animatedImage As System.Drawing.Bitmap
    Private m_cnt As Integer = 0

    Private Sub FormUpdate_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
    End Sub

    Private Sub UpdateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UpdateButton.Click
        Dim result As Boolean = False
        Dim ResultListViewItem As ListViewItem
        UpdateButton.Enabled = False
        EndButton.Enabled = False

        Try
            ResultListViewItem = AddListLine("ﾌｧｲﾙﾀﾞｳﾝﾛｰﾄﾞ")
            CFileLog.WriteLog("サーバーからZIPファイルをダウンロード--START", CFileLog.EnumLogType.DOWNLOAD, CFileLog.EnumLogType.DOWNLOAD)
            result = fileDownload(tmpFileName)
            If result Then
                CFileLog.WriteLog("サーバーからZIPファイルをダウンロード--OK", CFileLog.EnumLogType.DOWNLOAD, CFileLog.EnumLogType.DOWNLOAD)
                ResultListViewItem.SubItems(2).Text = "完"
                Dim fileName As String = CApplicationManager.GetRealPath(tmpFileName, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                Dim ZipTmpPath As String = CApplicationManager.GetRealPath(tmpPath, CApplicationManager.EnumSpecialFolder.PATH_MYDOCUMENT)
                Dim ProgramFilesPath As String = CApplicationManager.GetRealPath("", CApplicationManager.EnumSpecialFolder.PATH_APPLICATION)
                If System.IO.Directory.Exists(ZipTmpPath) Then
                    System.IO.Directory.Delete(ZipTmpPath, True)
                End If
                ResultListViewItem = AddListLine("ﾌｧｲﾙ展開")

                CFileLog.WriteLog("ZIPファイル解凍--START", CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                If unZipFile(fileName, ZipTmpPath) = "OK" Then
                    CFileLog.WriteLog("ZIPファイル解凍--OK", CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                    ResultListViewItem.SubItems(2).Text = "完"

                    CFileLog.WriteLog("ZipTmpPath:->" & ZipTmpPath, CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                    Dim root() As String = System.IO.Directory.GetDirectories(ZipTmpPath)
                    CFileLog.WriteLog("root().GetDirectories:->" & root.Length, CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                    'CFileLog.WriteLog("root(0):->" & root(0), CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                    'CFileLog.WriteLog("root(1):->" & root(1), CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                    If root.Length = 1 Then
                        Dim ErrorFlg As Boolean = False
                        Try
                            CFileLog.WriteLog("p1:-変換前>" & ProgramFilesPath, CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                            Dim p1 As String = ProgramFilesPath.Replace("\update", "")
                            CFileLog.WriteLog("p1:-変換後>" & p1, CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)

                            'CopyDirectory(ZipTmpPath, p1, True)
                            CFileLog.WriteLog("ダウンロードしたソース：" & ZipTmpPath, CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                            Dim srcFiles() As String = System.IO.Directory.GetFiles(root(0))

                            CFileLog.WriteLog("ソースsrcFiles.count：" & srcFiles.Length, CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                            CFileLog.WriteLog("コピーSTART--------", CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                            For j As Integer = 0 To srcFiles.Length - 1
                                Dim destFileName As String = System.IO.Path.Combine(p1, System.IO.Path.GetFileName(srcFiles(j)))
                                CFileLog.WriteLog("destFileName----->" & destFileName, CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                                If srcFiles(j).IndexOf("ICSharpCode.SharpZipLib.dll") = -1 And srcFiles(j).IndexOf("Update.exe") = -1 Then
                                    ResultListViewItem = AddListLine(System.IO.Path.GetFileName(srcFiles(j)))
                                    System.IO.File.Copy(srcFiles(j), destFileName, True)
                                    CFileLog.WriteLog("ソース-->" & srcFiles(j) & "\\\\ コピー:" & destFileName, CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                                    ResultListViewItem.SubItems(2).Text = "完"
                                End If
                            Next
                            CFileLog.WriteLog("コピーEND--------", CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                        Catch ex As Exception
                            ErrorFlg = True
                            ResultListViewItem.SubItems(2).Text = "ER"
                        End Try

                        If Not ErrorFlg Then
                            ResultListViewItem = AddListLine("正常終了-----")
                        Else
                            ResultListViewItem = AddListLine("異常終了")
                            UpdateButton.Enabled = True
                        End If
                    End If
                Else
                    CFileLog.WriteLog("ZIPファイル解凍--失敗", CFileLog.EnumLogType.READ, CFileLog.EnumLogType.READ)
                    ResultListViewItem = AddListLine("異常終了")
                    UpdateButton.Enabled = True
                End If
            Else
                ResultListViewItem = AddListLine("異常終了")
                UpdateButton.Enabled = True
            End If
            MsgBox("更新が完了しました。")
        Catch ex As Exception
            ResultListViewItem = AddListLine("異常終了")
            UpdateButton.Enabled = True
        End Try


        EndButton.Enabled = True
        EndButton.Focus()
    End Sub

    Private Function AddListLine(ByVal msg As String) As ListViewItem
        Dim itm As New ListViewItem
        Dim tgt As New ListViewItem.ListViewSubItem
        Dim res As New ListViewItem.ListViewSubItem

        itm.SubItems.Add(tgt)
        itm.SubItems.Add(res)

        m_cnt += 1
        itm.Text = CStr(m_cnt)
        tgt.Text = msg

        ResultListView.BeginUpdate()
        ResultListView.Items.Add(itm)
        ResultListView.EndUpdate()

        Return itm
    End Function

    Private Sub EndButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EndButton.Click
        Dim app As CApplicationManager = CApplicationManager.GetInstance()


        app.ExecuteUpdateProcess("LeaderEye.exe")
        Me.Close()
    End Sub
End Class